package com.itspeedster.betterforeyes;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class CounterDialog extends JDialog{
	
	int time_counter;
	JLabel lbCounter;
	JLabel lbAlert;
	JFrame parent;
	
	public CounterDialog(JFrame parent){
		super(parent, "Better for eyes", true);
		this.parent = parent;
		time_counter = AppLauncher.config.relaxTime;
		
		addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e){
				lbAlert.setForeground(Color.RED);
				lbAlert.setText("Không đùa đâu!");
			}
		});
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		setLayout(new FlowLayout(FlowLayout.CENTER));
		setSize(400, 130);
		JLabel lb1 = new JLabel("Thư giãn một chút nào!");
		lb1.setHorizontalAlignment(JLabel.CENTER);
		
		lb1.setPreferredSize(new Dimension(400, 25));
		add(lb1);
		
		lbCounter = new JLabel();
		lbCounter.setForeground(Color.BLUE);
		lbCounter.setPreferredSize(new Dimension(400, 25));
		lbCounter.setHorizontalAlignment(JLabel.CENTER);
		add(lbCounter);
		
		lbAlert = new JLabel("Đừng khiến mình trở nên căng thẳng!");
		lbAlert.setPreferredSize(new Dimension(400, 25));
		lbAlert.setHorizontalAlignment(JLabel.CENTER);
		add(lbAlert);
		
		
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(time_counter >0){
					lbCounter.setText(time_counter+"");
					try{
						Thread.sleep(1000);
					}catch(InterruptedException ex){
						
					}
					time_counter--;
					System.out.println("counter = " + time_counter);
				}
				dispose();
				CounterDialog.this.parent.dispose();
			}
		}).start();
	

		
		setLocationRelativeTo(parent);
		setVisible(true);
	}
}
